package com.kang.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.AuditFlowItemMapper;
import com.kang.pojo.AuditFlowItem;
import com.kang.service.AuditFlowItemService;

/**
 * 工作流程--流程级别（实现类）
 * @author wd
 *
 */
@Service
public class AuditFlowItemServiceImpl implements AuditFlowItemService {
	/**
	 * 工作流程--流程级别DAO
	 */
	@Resource
	private AuditFlowItemMapper auditFlowItemMapper;

	/**
	 * 根据审批流程id查询所属的流程级别集合list
	 */
	@Override
	public List<AuditFlowItem> getAuditFlowItemListByAuditFlowID(int auditFlowID) {
		List<AuditFlowItem> newAuditFlowItemList = null;
		List<AuditFlowItem> auditFlowItemList = auditFlowItemMapper.getAuditFlowItemListByAuditFlowID(auditFlowID);
		if(auditFlowItemList!=null){
			newAuditFlowItemList = new ArrayList<AuditFlowItem>();
			int listSize = auditFlowItemList.size();
			for (int i = 0; i < listSize; i++) {
				AuditFlowItem auditFlowItem = auditFlowItemList.get(i);
				List<String> userString = auditFlowItemMapper.getAuditFlowItemUserNameById(auditFlowItem.getId());
				auditFlowItem.setUserListName(userString==null?"":userString.toString().replace("[","").replace("]", "").replace("null", ""));
				auditFlowItem.setMaxPassPersonNumber(userString==null?0:userString.size());
				newAuditFlowItemList.add(auditFlowItem);
			}
		}
		if(newAuditFlowItemList!=null){
			return newAuditFlowItemList;
		}else{
			return auditFlowItemList;
		}
	}

	/**
	 * 根据角色id查询使用该角色的用户
	 */
	@Override
	public AuditFlowItem queryRoleUserByRoleId(int roleID) {
		List<String> userString = auditFlowItemMapper.queryRoleUserListByRoleId(roleID);
		AuditFlowItem auditFlowItem = new AuditFlowItem();
		auditFlowItem.setUserListName(userString==null?"":userString.toString().replace("[","").replace("]", "").replace("null", ""));
		auditFlowItem.setPassPersonNumber(userString==null?0:userString.size());
		return auditFlowItem;
	}

	/**
	 * 根据审核流程id删除所属的审批级别
	 */
	@Override
	public int deleteAuditFlowItemForAuditFlowId(Integer auditFlowID) {
		return auditFlowItemMapper.deleteAuditFlowItemForAuditFlowId(auditFlowID);
	}

	/**
	 * 新增审批级别到审批流程
	 */
	@Override
	public int addAuditFlowItemToAuditFlow(Integer auditFlowID,
			Integer[] level_S, Integer[] roleID_S, Integer[] passPersonNumber_S) {
		if(level_S!=null){
			for (int i = 0; i < level_S.length; i++) {
				AuditFlowItem audit = new AuditFlowItem();
				audit.setAuditFlowID(auditFlowID);
				audit.setLevel(level_S[i]);
				audit.setRoleID(roleID_S[i]);
				audit.setPassPersonNumber(passPersonNumber_S[i]);
				auditFlowItemMapper.insertNewAuditFlowItem(audit);
			}
			return level_S.length;
		}
		return 0;
	}

}
